import { createStore } from 'vuex'

const store = createStore({
  state: {
    rooms: [
      {
        id: 1,
        name: '客厅',
        devices: [
          { 
            id: 1, 
            name: '主灯', 
            type: 'light', 
            status: true,
            brightness: 100 
          },
          { 
            id: 2, 
            name: '空调', 
            type: 'ac', 
            status: true,
            temperature: 26,
            mode: 'cool',
            fanSpeed: 'auto'
          },
          { 
            id: 3, 
            name: '窗帘', 
            type: 'curtain', 
            status: true,
            openness: 100 
          }
        ]
      },
      {
        id: 2,
        name: '主卧',
        devices: [
          { 
            id: 4, 
            name: '吸顶灯', 
            type: 'light', 
            status: true,
            brightness: 100 
          },
          { 
            id: 5, 
            name: '空调', 
            type: 'ac', 
            status: true,
            temperature: 24,
            mode: 'cool',
            fanSpeed: 'auto'
          },
          { 
            id: 6, 
            name: '窗帘', 
            type: 'curtain', 
            status: true,
            openness: 100 
          }
        ]
      },
      {
        id: 3,
        name: '厨房',
        devices: [
          { id: 7, name: '射灯', type: 'light', status: true },
          { id: 8, name: '冰箱', type: 'fridge', status: true }
        ]
      },
      {
        id: 4,
        name: '卫生间',
        devices: [
          { id: 9, name: '顶灯', type: 'light', status: true },
          { id: 10, name: '热水器', type: 'waterHeater', status: true }
        ]
      }
    ],
    deviceTypes: [
      { id: 'light', name: '灯光' },
      { id: 'ac', name: '空调' },
      { id: 'curtain', name: '智能窗帘' },
      { id: 'fridge', name: '冰箱' },
      { id: 'waterHeater', name: '热水器' }
    ],
    theme: {
      isDark: false,
      primaryColor: '#409EFF'
    },
    automationTasks: [
      {
        id: 1,
        name: '晚上关灯',
        deviceId: 1,
        deviceName: '客厅灯',
        time: '22:00',
        action: 'off',
        enabled: true
      },
      {
        id: 2,
        name: '早上开窗帘',
        deviceId: 3,
        deviceName: '智能窗帘',
        time: '07:00',
        action: 'on',
        enabled: true
      }
    ]
  },
  mutations: {
    updateDeviceStatus(state, { deviceId, status }) {
      state.rooms.forEach(room => {
        const device = room.devices.find(d => d.id === deviceId)
        if (device) {
          device.status = status
        }
      })
    },
    updateDeviceSetting(state, { deviceId, setting, value }) {
      state.rooms.forEach(room => {
        const device = room.devices.find(d => d.id === deviceId)
        if (device) {
          device[setting] = value
        }
      })
    },
    addRoom(state, room) {
      state.rooms.push(room)
    },
    removeRoom(state, roomId) {
      const index = state.rooms.findIndex(r => r.id === roomId)
      if (index !== -1) {
        state.rooms.splice(index, 1)
      }
    },
    addDevice(state, { roomName, device }) {
      const room = state.rooms.find(r => r.name === roomName)
      if (room) {
        const maxId = Math.max(...state.rooms.reduce((ids, r) => {
          return ids.concat(r.devices.map(d => d.id))
        }, [0]))
        
        room.devices.push({
          id: maxId + 1,
          status: false,
          ...device
        })
      }
    },
    removeDevice(state, { roomName, deviceId }) {
      const room = state.rooms.find(r => r.name === roomName)
      if (room) {
        const index = room.devices.findIndex(d => d.id === deviceId)
        if (index !== -1) {
          room.devices.splice(index, 1)
        }
      }
    },
    setTheme(state, { isDark, primaryColor }) {
      state.theme.isDark = isDark
      state.theme.primaryColor = primaryColor
      const html = document.documentElement
      if (isDark) {
        html.classList.add('dark-theme')
      } else {
        html.classList.remove('dark-theme')
      }
      html.style.setProperty('--el-color-primary', primaryColor)
    },
    addAutomationTask(state, task) {
      state.automationTasks.push({
        id: state.automationTasks.length + 1,
        ...task,
        enabled: true
      })
    },
    toggleAutomationTask(state, { taskId, enabled }) {
      const task = state.automationTasks.find(t => t.id === taskId)
      if (task) {
        task.enabled = enabled
      }
    },
    removeAutomationTask(state, taskId) {
      const index = state.automationTasks.findIndex(t => t.id === taskId)
      if (index !== -1) {
        state.automationTasks.splice(index, 1)
      }
    }
  },
  actions: {
    toggleDevice({ commit }, { deviceId, status }) {
      commit('updateDeviceStatus', { deviceId, status })
    },
    updateDeviceSetting({ commit }, { deviceId, setting, value }) {
      commit('updateDeviceSetting', { deviceId, setting, value })
    },
    addDevice({ commit }, payload) {
      commit('addDevice', payload)
    },
    removeDevice({ commit }, payload) {
      commit('removeDevice', payload)
    },
    updateTheme({ commit }, theme) {
      commit('setTheme', theme)
    },
    addAutomationTask({ commit }, task) {
      commit('addAutomationTask', task)
    },
    toggleAutomationTask({ commit }, payload) {
      commit('toggleAutomationTask', payload)
    },
    removeAutomationTask({ commit }, taskId) {
      commit('removeAutomationTask', taskId)
    }
  },
  getters: {
    devicesByRoom: (state) => (roomName) => {
      const room = state.rooms.find(r => r.name === roomName)
      return room ? room.devices : []
    },
    devicesByType: (state) => (type) => {
      return state.rooms.reduce((acc, room) => {
        return acc.concat(room.devices.filter(device => device.type === type))
      }, [])
    },
    activeDevices: (state) => {
      return state.rooms.reduce((acc, room) => {
        return acc.concat(room.devices.filter(device => device.status))
      }, [])
    },
    allDevices: (state) => {
      return state.rooms.reduce((acc, room) => {
        return acc.concat(room.devices.map(device => ({
          ...device,
          room: room.name
        })))
      }, [])
    },
    automationTasks: (state) => state.automationTasks
  }
})

export default store
